package com.zsrt.javaweb;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 对默认的Filter进行了加强处理
 * @author zsrt
 *
 */
public abstract class HttpFilter implements Filter {
	
	
	private FilterConfig fc;

	public void init(FilterConfig config) throws ServletException {
		System.out.println("进入父类的init方法...");
		this.fc = config;
		initChildren();
	}
	
	/**
	 * 子类如果有需要才重写的初始化方法 比如拿配置文件的参数 context-param  init-param...
	 * 想想为啥用空的方法 而不用抽象的方法?
	 */
	public  void initChildren(){};
	
	/**
	 * 供子类继承的方法 用来获取FilterConfig对象
	 * @return
	 */
	public FilterConfig getFilterConfig(){
		
		return this.fc;
	}
	

	/**
	 * 原生的方法
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		
		System.out.println("doFilter...");
		
		HttpServletRequest request =  (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		
		doHttpFilter(request, response, chain);
	}
	
	
	/**
	 * 该方法将由子类重写 直接处理http请求
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 */
	public abstract void doHttpFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException; 

	
	
	public void destroy() {

	}

}
